package day14;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

//取ArrayList中的重复的元素
public class ArrayListTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("java01");
		list.add("java02");
		list.add("java01");
		list.add("java03");
		list.add("java02");
		list.add("java04");
		list.add("java03");
		SingleArrayList<String> single = new SingleArrayList<String>();
		list = single.singleElement(list);
		for (String string : list) {
			System.out.println(string);
		}
	}
	
	

}
//单独的封装成一个方法，去除ArrayList中的重复元素
class SingleArrayList<E> {
	
	public List<E> singleElement(List<E> list) {
		
		List<E> newList = new ArrayList<E>();
		for (ListIterator<E> iterator = list.listIterator(); iterator.hasNext();) {
			E e = iterator.next();
			if (!newList.contains(e)) {
				newList.add(e);
			}
		}
		return newList;
	}
}

